import { Injectable }     from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
//import { Observable } from 'rxjs';
import 'rxjs/add/operator/toPromise';

import { BaseService, Utils, PagedQueryResult } from 'iisp-ng';

import { DataTable,TableDataVO, DataInputState, DataInputDate,DataDetail ,Approver} from './dm.model';

declare var restRoot : string;

@Injectable()
export class DataManagerService extends BaseService{
	private dataMgrUrl = restRoot + '/iisp-data-mgr';  // URL to rest api
	private approverUrl = restRoot + '/iisp-auth/functions/IISP_DATA_INPUT_DATE_AUDIT/users';  // URL to rest api
	
	
	constructor(http: Http) { super(http); }
	
	getTables():Promise<DataTable[]>{
	    let upUrl : string = this.dataMgrUrl + "/data-tables";
        return this.doGet(upUrl);
	}
	getDataTable(tableName: string):Promise<DataTable>{
        let url : string = `${this.dataMgrUrl}/data-tables/${tableName}`;
        return this.doGet(url);
    }
	
   getApprovers():Promise<Approver[]>{
     
        return this.doGet(this.approverUrl);
    }
	
	getDataInputStates(dataDate: string):Promise<DataInputState[]>{
        let upUrl : string = `${this.dataMgrUrl}/data-input-states?dataDate=${dataDate}`;
        return this.doGet(upUrl);
    }
	getDataTableData(tableCode: string, param: any):Promise<PagedQueryResult<any>>{
	    let upUrl : string = this.dataMgrUrl + "/datas/data-tables/"+tableCode;
        return this.doGet(upUrl, param);
	}
	
	getDataInputStateDetail(sysCode:string,dataDate: string):Promise<DataDetail>{
        let upUrl : string = `${this.dataMgrUrl}/data-input-states/${sysCode}/${dataDate}`;
        return this.doGet(upUrl);
    }
	
	saveDateInputDates(dataInputDate:DataInputDate, action: string):Promise<DataInputDate>{
	    let upUrl : string = `${this.dataMgrUrl}/data-input-dates?action=${action}`;
	    return this.doPut(upUrl, dataInputDate);
	}
   addDateInputDates(dataInputDate:DataInputDate, action: string):Promise<DataInputDate>{
        let upUrl : string = `${this.dataMgrUrl}/data-input-dates?action=${action}`;
        return this.doPost(upUrl, dataInputDate);
    }
	
	getDataInputDates(filter: any): Promise<DataInputDate[]> {
        let upUrl : string = `${this.dataMgrUrl}/data-input-dates`;
	    return this.doGet(upUrl);
	}
	
	disableDataInputDate(dataInputDate:DataInputDate, action: string):Promise<DataInputDate>{
        let upUrl : string = `${this.dataMgrUrl}/disable-data-input-dates?action=${action}`;
        return this.doPut(upUrl, dataInputDate);
    }
}